package com.tensquare.rest;

import com.tensquare.entity.Result;
import com.tensquare.entity.Spit;
import com.tensquare.entity.StatusCode;
import com.tensquare.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 吐槽控制器
 */
@RestController
@RequestMapping("spit")
public class SpitRest {

    @Autowired
    SpitService spitService;

    /**
     * 分页查询所有的吐槽记录
     * http://localhost:9005/spit/findAll/1/10
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "/list/{page}/{limit}", method = RequestMethod.GET)
    public Result list(@PathVariable("page") int page, @PathVariable("limit") int limit){

        return new Result(true, StatusCode.OK, "查询成功",spitService.findAll(page, limit) );
    }

    /**
     * 根据id查询吐槽记录
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable("id") String id){

        return new Result(true, StatusCode.OK, "查询成功",spitService.findById(id) );
    }

    /**
     * 根据上级id查询吐槽记录
     * @param page
     * @param limit
     * @param parentid
     * @return
     */
    @RequestMapping(value = "/findByParentid/{partentid}/{page}/{limit}", method = RequestMethod.GET)
    public Result findByParentid(@PathVariable("page") int page,
                                  @PathVariable("limit") int limit,
                                  @PathVariable("partentid") String parentid){

        return new Result(true, StatusCode.OK, "查询成功",spitService.findByParentid(parentid, page, limit) );
    }

    /**
     * 新增吐槽记录
     * @param
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Result add(@RequestBody Spit spit){//
        spitService.add(spit);
        return new Result(true, StatusCode.OK, "新增成功" );
    }

    /**
     * 根据id修改记录
     * @param spit
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody Spit spit, @PathVariable("id") String id){
        spit.setId(id);
        spitService.update(spit);
        return new Result(true, StatusCode.OK, "修改成功" );
    }

    /**
     * 文章点赞
     * @param id
     * @return
     */
    @RequestMapping(value = "/thumbup/{id}", method = RequestMethod.PUT)
    public Result thumbup( @PathVariable("id") String id){

        spitService.updateThumbup(id);
        return new Result(true, StatusCode.OK, "点赞成功" );
    }

    /**
     * 根据id删除吐槽记录
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") String id){
        spitService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功" );
    }
}
